/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import util.Package;

/**
 *
 * @author Vlada, Aleksandar
 */
public class Client {

    static int port = 3070;
    static InetAddress host;

    public static void main(String[] args) {

        boolean ipIsOk = false;
        System.out.println("*** Klijent je pokrenut");
       
        while (!ipIsOk) {
            try {
                host = InetAddress.getByName(JOptionPane.showInputDialog("IP Adresa: "));
                ipIsOk = true;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Morate uneti IP adresu u ispravnom formatu!");
            }
        }

        System.out.println("*** Odabrana IP Adresa: " + host.getHostAddress() + "\n*** Port: " + port);
        try {
            Socket socket = new Socket(host.getHostAddress(), port);

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            int wServer = ois.readInt();
            int hServer = ois.readInt();

            int wClient = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
            int hClient = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();

            oos.writeInt(wClient);
            oos.writeInt(hClient);
            oos.flush();

            int wFinal = ois.readInt();
            int hFinal = ois.readInt();
            double percent = ois.readDouble();

            ClientForm forma = new ClientForm(wFinal, hFinal, host.getHostAddress(), percent);
            forma.setVisible(true);

            while (true) {
                int numberOfBytesInImage = ois.readInt();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int numberOfPackages = numberOfBytesInImage / 1024;
                int rest = numberOfBytesInImage % 1024;
                for (int j = 0; j < numberOfPackages; j++) {
                    Package pob = (Package) ois.readObject();
                    baos.write(pob.getBytes());
                }
                Package pob = (Package) ois.readObject();
                baos.write(pob.getBytes());
                byte[] bytesOfImage = baos.toByteArray();
                ImageIcon icon = new ImageIcon(bytesOfImage);
                forma.setIcon(icon);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    System.err.println(ex.getMessage());
                }
            }
        } catch (ClassNotFoundException | IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
